import socket
import threading

# 创建TCP/IP套接字
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 服务器地址和端口号
server_address =('117.68.45.58', 19789)

# 连接服务器
client_socket.connect(server_address)
client_socket.sendall("client".encode("utf-8"))
Controller = threading.Lock()


def receiver(client_socket):
    while True:
        mesg = client_socket.recv(1024)
        if mesg.decode() == '': break
        print(mesg.decode("utf-8"))
        if Controller.locked():Controller.release()


def sender(client_socket):
    while True:

        print("请输入内容：")
        mesg = input()
        if Controller.locked():
            print("正在等待回复，请稍后...\n")
            if mesg == 'exit': break
            continue
        client_socket.send(mesg.encode("utf-8"))
        print("发送成功,等待回复...")
        if mesg == 'exit': break
        Controller.acquire()



receiver_thread = threading.Thread(target=receiver, args=(client_socket,))
sender_thread = threading.Thread(target=sender, args=(client_socket,))
receiver_thread.start()
sender_thread.start()
